﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Qiao.Blog
{
    public class QiaoBlogMigrationsDbContextFactory : IDesignTimeDbContextFactory<QiaoBlogMigrationsDbContext>
    {
        public QiaoBlogMigrationsDbContext CreateDbContext(string[] args)
        {
            var configuration = BuildConfiguration();

            var EnableDb = configuration["ConnectionStrings:Enable"];
    
            var builder = new DbContextOptionsBuilder<QiaoBlogMigrationsDbContext>();

            switch (EnableDb)
            {
                case "MySql":
                    //builder.UseMySql(configuration.GetConnectionString(EnableDb));
                    break;

                case "SqlServer":
                    builder.UseSqlServer(configuration.GetConnectionString(EnableDb));
                    break;

                case "PostgreSql":
                    builder.UseNpgsql(configuration.GetConnectionString(EnableDb));
                    break;

                case "Sqlite":
                    builder.UseSqlite(configuration.GetConnectionString(EnableDb));
                    break;
            }

            return new QiaoBlogMigrationsDbContext(builder.Options);
        }

        private static IConfigurationRoot BuildConfiguration()
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

            return builder.Build();
        }
    }
}
